<template>
  <div class='container-subjects'>
    <el-card>
      <!-- 筛选 -->
     <el-row>
      <!-- 表单 -->
      <el-col :span="18">
       <el-form label-width="80px" inline size="small">
       <el-form-item label="学科名称">
         <el-input v-model="reqParams.subjectName" placeholder="搜索关键字"></el-input>
         </el-form-item>
          <el-form-item>
            <el-button @click="clear()">清除</el-button>
            <el-button type="primary" @click="search()">搜索</el-button>
          </el-form-item>
         </el-form>
         </el-col>
      <!-- 按钮 -->
      <el-button style="float:right" type="success" icon="el-icon-edit" size="small">添加学科</el-button>
      <el-col :span="6"></el-col>
    </el-row>
      <!-- 总计 -->
      <el-alert :title="`数据一共 ${total} 条`" type="info" :closable="false" show-icon style="margin-bottom:15px"></el-alert>
      <!-- 表格 -->
      <el-table :data="subjects">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="学科名称" prop="subjectName"></el-table-column>
        <el-table-column label="创建者" prop="username"></el-table-column>
        <el-table-column label="创建日期" width="170px">
          <template slot-scope="scope">
            {{scope.row.addDate|parseTimeByString}}
          </template>
        </el-table-column>
        <el-table-column label="前台是否显示">
           <template slot-scope="scope">
            {{scope.row.isFrontDisplay === 1? '是':'否'}}
          </template>
        </el-table-column>
        <el-table-column label="二级目录" prop="twoLevelDirectory"></el-table-column>
        <el-table-column label="标签" prop="tags"></el-table-column>
        <el-table-column label="题目数量" prop="totals"></el-table-column>
        <el-table-column label="操作" width="240">
           <template slot-scope="scope">
             <el-link @click="toDiretory(scope.row)" :underline="false" type="primary">学科分类</el-link>
             <el-link @click="toTag(scope.row)" :underline="false" type="primary">学科标签</el-link>
             <el-link @click="openDialog(scope.row)" :underline="false" type="primary">修改</el-link>
             <el-link @click="delSubject(scope.row.id)" :underline="false" type="primary">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination background class="pager"
      layout="prev, pager, next, sizes, jumper"
      :total="total"
      :page-size="reqParams.pagesize"
      :current-page="reqParams.page"
      @current-change="changePager"
      @size-change="changeSize"
      :page-sizes="[5,10,20,30]"
      ></el-pagination>
      <!-- 分页 五页 十页 二十页 五十页 尺码选择-->
      <!-- 监听页码尺寸改变  -->
    </el-card>
    <!-- 添加编辑学科组件 -->
    <subjects-add @upateList="upateList" :subject="currSubject" ref="subjectsAdd"></subjects-add>
  </div>
</template>

<script>
// 学科列表API
import { list as subjectsList, remove as subjectsDel } from '@/api/hmmm/subjects.js'
// 导入组件
import SubjectsAdd from '../components/subjects-add.vue'
export default {
  name: 'subjects-page',
  components: { SubjectsAdd },
  data () {
    return {
      // 筛选参数
      reqParams: {
        // 学科名字,关键字
        subjectName: null,
        // 页码
        page: 1,
        // 尺寸
        pagesize: 10
      },
      // 学科列表
      subjects: [],
      // 总条数
      total: 0,
      // 当前的subject 数据是{} 就是添加 数据是{id,subjectName,...}就是修改
      currSubject: {}
    }
  },
  created () {
    this.getSubjects()
  },
  methods: {
    // 删除学科
    delSubject (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await subjectsDel({ id })
        this.$message.success('删除成功')
        this.getSubjects()
      }).catch(e => {})
    },
    // 更新列表
    upataList () {
      // 如果新增就到第一列第一个
      if (!this.currSubject.id) {
        this.reqParams.page = 1
      }
      this.getSubjects()
    },
    // 打开对话框
    openDialog (row) {
      // 数据是{}就是添加 数据是{id.subjectName,...}就是修改
      this.currSubject = row
      // 调用 subjects-add 组件的open函数 打开对话框
      this.$nextTick(() => {
        this.$refs.subjectsAdd.open()
      })
    },
    // 目录跳转
    toDiretory (row) {
      const { id, subjectName: name } = row
      // 目录按点击的id取值
      this.$router.push({ path: 'directorys', query: { id, name } })
    },
    // 标签跳转
    toTag (row) {
      const { id, subjectName: name } = row
      // 标签按点击的id取值
      this.$router.push({ path: 'tags', query: { id, name } })
    },
    // 获取科学列表
    async getSubjects () {
      // api目录中的函数和接口和数据相关 api函数是以对象方式传递
      const { data } = await subjectsList(this.reqParams)
      // 给data中的数据赋值
      this.subjects = data.items
      this.total = data.counts
    },
    // 分页
    changPager (newPage) {
      this.reqParams.page = newPage
      this.gerSubjects()
    },
    // 尺码
    changeSize (newSize) {
      this.reqParams.page = 1
      this.reqParams.pagesize = newSize
      this.getSubjects()
    },
    // 搜索
    search () {
      // 在此业务中关键字是双向绑定的 如果其他业务有需要改动的参数在搜索前改动
      this.reqParams.page = 1
      this.gerSubjects()
    },
    // 清空
    clear () {
      this.reqParams.subjectName = null
    }
  }
}
</script>

<style scoped lang='scss'>
.container-subjects{
  padding: 10px 15px;
  .pager{
    margin-top: 15px;
    margin-bottom: 15px;
    float: right;
  }
}
.el-link{
  margin-right: 10px;
}
</style>
